package com.sjgtw.cloud.system.controller;

import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxProvisionResult;
import com.sjgtw.cloud.common.log.annotation.Log;
import com.sjgtw.cloud.common.log.enums.BusinessType;
import com.sjgtw.cloud.system.api.domain.SysDept;
import com.sjgtw.cloud.system.domain.orm.OrgEntity;
import com.sjgtw.cloud.system.domain.vo.ProvisioningVo;
import com.sjgtw.cloud.system.service.ISysDeptService;
import com.sjgtw.cloud.system.service.ISysOrganizationProvisioningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>Title: 用户数据同步接口实现</p>
 * <p>Description: 门户同步器接口实现</p>
 * @author Admin
 * @date 2022年7月25日
 */
@RestController
@RequestMapping("/provisioning/rsf/organization")
public class ProvisioningOrganizationRSFController extends BaseController
{
    @Autowired
    private ISysOrganizationProvisioningService orgService;

	@Autowired
	private ISysDeptService deptService;

    /**
     * <p>Title: 创建新组织机构数据</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "组织机构数据同步(rsf)", businessType = BusinessType.INSERT)
    @PostMapping("/addOrganization")
    public AjaxProvisionResult add(@RequestBody ProvisioningVo ps)
    {
    	OrgEntity org = ps.getOrgEntity();
    	SysDept dept = deptService.selectDeptByDeptNum(org.getOrgNum());
    	//修改组织机构
    	if (StringUtils.isNotNull(dept))
    	{
    		dept.setDeptNum(org.getOrgNum());
        	dept.setDeptCode(org.getOrgCode());
        	dept.setDeptName(org.getOrgName());
        	dept.setFullName(org.getFullName());
        	dept.setParentId(Long.valueOf(org.getParentId()));
        	dept.setDeptType(org.getOrgType());
        	dept.setStatus(org.getStatus());
    		dept.setUpdateBy("rsf组织机构同步");
    		dept.setUpdateTime(ps.getTimeStamp());
    		int rows = orgService.updateDept(dept);
    		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"新增组织机构成功")
    				: AjaxProvisionResult.error(ps.getRequestID(),"orgexception-002","新增组织机构出现错误");
    	}
    	dept = new SysDept();
    	dept.setDeptNum(org.getOrgNum());
    	dept.setDeptCode(org.getOrgCode());
    	dept.setDeptName(org.getOrgName());
    	dept.setFullName(org.getFullName());
    	dept.setParentId(Long.valueOf(org.getParentId()));
    	dept.setDeptType(org.getOrgType());
    	dept.setStatus(org.getStatus());
		dept.setCreateBy("rsf组织机构同步");
    	dept.setCreateTime(ps.getTimeStamp());
    	int rows = orgService.insertDept(dept);
      	return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"新增组织机构成功")
  				: AjaxProvisionResult.error(ps.getRequestID(),"orgexception-002","新增组织机构出现错误");

    }

    /**
     * <p>Title: 修改组织机构信息</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "组织机构数据同步(rsf)", businessType = BusinessType.UPDATE)
    @PutMapping("/modifyOrganization")
    public AjaxProvisionResult edit(@RequestBody ProvisioningVo ps)
    {
    	OrgEntity org = ps.getOrgEntity();
    	SysDept dept = deptService.selectDeptByDeptNum(org.getOrgNum());
    	//修改组织机构
    	if (StringUtils.isNotNull(dept))
    	{
    		dept.setDeptNum(org.getOrgNum());
        	dept.setDeptCode(org.getOrgCode());
        	dept.setDeptName(org.getOrgName());
        	dept.setFullName(org.getFullName());
        	dept.setParentId(Long.valueOf(org.getParentId()));
        	dept.setDeptType(org.getOrgType());
        	dept.setStatus(org.getStatus());
    		dept.setUpdateBy("rsf组织机构同步");
        	dept.setUpdateTime(ps.getTimeStamp());
        	int rows = orgService.updateDept(dept);
          	return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"修改组织机构成功")
      				: AjaxProvisionResult.error(ps.getRequestID(),"orgexception-003","修改组织机构出现错误");

    	}
    	dept = new SysDept();
    	dept.setDeptNum(org.getOrgNum());
    	dept.setDeptCode(org.getOrgCode());
    	dept.setDeptName(org.getOrgName());
    	dept.setFullName(org.getFullName());
    	dept.setParentId(Long.valueOf(org.getParentId()));
    	dept.setDeptType(org.getOrgType());
    	dept.setStatus(org.getStatus());
    	dept.setCreateBy("rsf组织机构同步");
		dept.setCreateTime(ps.getTimeStamp());
		int rows = orgService.insertDept(dept);
		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"修改组织机构成功")
				: AjaxProvisionResult.error(ps.getRequestID(),"orgexception-003","修改组织机构出现错误");
    }

    /**
     * <p>Title: 删除组织机构信息</p>
     * <p>Description: </p>
     * @param ProvisioningVo
     * @return
     */
    @Log(title = "组织机构数据同步(rsf)", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteOrganization")
    public AjaxProvisionResult delete(@RequestBody ProvisioningVo ps)
    {
        OrgEntity org = ps.getOrgEntity();
        SysDept dept = deptService.selectDeptByDeptNum(org.getOrgNum());
    	//逻辑删除组织机构
    	if (StringUtils.isNull(dept))
    	{
    		return AjaxProvisionResult.success(ps.getRequestID(),"删除组织机构成功");
    	}
    	int rows = orgService.deleteDeptById(dept.getDeptId());
		return rows > 0 ? AjaxProvisionResult.success(ps.getRequestID(),"删除组织机构成功")
				: AjaxProvisionResult.error(ps.getRequestID(),"orgexception-004","删除组织机构出现错误");
    }
}
